/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hookedonplay.decoviewsample;

import com.hookedonplay.decoviewlib.DecoView;
import com.hookedonplay.decoviewlib.PointF;
import com.hookedonplay.decoviewlib.charts.EdgeDetail;
import com.hookedonplay.decoviewlib.charts.SeriesItem;
import com.hookedonplay.decoviewlib.charts.SeriesLabel;
import com.hookedonplay.decoviewlib.events.DecoEvent;
import com.hookedonplay.decoviewsample.ResourceTable;
import com.hookedonplay.decoviewsample.Utility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.ShapeElement;

public class SamplePeopleAbility extends Ability {
    final float mSeriesMax = 100f;
    private int mSeries1Index;
    private int mSeries2Index;
    private int mSeries3Index;
    private int mBack1Index;

    final private int COLOR_BLUE = RgbPalette.parse("#AA1D76D2");
    final private int COLOR_PINK = RgbPalette.parse("#AAFF4081");
    final private int COLOR_YELLOW = RgbPalette.parse("#AAFFC107");
    final private int COLOR_EDGE = RgbPalette.parse("#22000000");
    final private int COLOR_BACK = RgbPalette.parse("#22888888");

    private DecoView decoView;
    private Image imageViewAvatar;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sample_people);

        decoView = (DecoView) findComponentById(ResourceTable.Id_decoView);
        imageViewAvatar = (Image) findComponentById(ResourceTable.Id_imageViewAvatar);

        createTracks();
        setupEvents();
    }

    private void createTracks() {
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(128, 218, 128));
        decoView.setBackground(element);

        decoView.executeReset();
        decoView.deleteAll();

        float circleInset = Utility.getDimension(23, getApplicationContext()) - (Utility.getDimension(46, getApplicationContext()) * 0.3f);
        SeriesItem seriesBack1Item = new SeriesItem.Builder(COLOR_BACK)
                .setRange(0, mSeriesMax, mSeriesMax)
                .setChartStyle(SeriesItem.ChartStyle.STYLE_PIE)
                .setInset(new PointF(circleInset, circleInset))
                .build();

        mBack1Index = decoView.addSeries(seriesBack1Item);

        SeriesItem series1Item = new SeriesItem.Builder(COLOR_BLUE)
                .setRange(0, mSeriesMax, 0)
                .setInitialVisibility(false)
                .setLineWidth(Utility.getDimension(46, getApplicationContext()))
                .setSeriesLabel(new SeriesLabel.Builder("Men").build())
                .setCapRounded(false)
                .addEdgeDetail(new EdgeDetail(EdgeDetail.EdgeType.EDGE_INNER, COLOR_EDGE, 0.3f))
                .setShowPointWhenEmpty(false)
                .build();

        mSeries1Index = decoView.addSeries(series1Item);

        SeriesItem series2Item = new SeriesItem.Builder(COLOR_PINK)
                .setRange(0, mSeriesMax, 0)
                .setInitialVisibility(false)
                .setLineWidth(Utility.getDimension(46, getApplicationContext()))
                .setSeriesLabel(new SeriesLabel.Builder("Women").build())
                .setCapRounded(false)
                .addEdgeDetail(new EdgeDetail(EdgeDetail.EdgeType.EDGE_INNER, COLOR_EDGE, 0.3f))
                .setShowPointWhenEmpty(false)
                .build();

        mSeries2Index = decoView.addSeries(series2Item);

        SeriesItem series3Item = new SeriesItem.Builder(COLOR_YELLOW)
                .setRange(0, mSeriesMax, 0)
                .setInitialVisibility(false)
                .setLineWidth(Utility.getDimension(46, getApplicationContext()))
                .setSeriesLabel(new SeriesLabel.Builder("Children").build())
                .setCapRounded(false)
                .addEdgeDetail(new EdgeDetail(EdgeDetail.EdgeType.EDGE_INNER, COLOR_EDGE, 0.3f))
                .setShowPointWhenEmpty(false)
                .build();

        mSeries3Index = decoView.addSeries(series3Item);
    }

    private void setupEvents() {
        decoView.executeReset();

        imageViewAvatar.setImageElement(null);
        imageViewAvatar.setVisibility(Component.INVISIBLE);

        addAnimation(decoView, mSeries1Index, 19, 3000, imageViewAvatar, ResourceTable.Media_ic_avatar_man, COLOR_BLUE);
        addAnimation(decoView, mSeries2Index, 45, 11000, imageViewAvatar, ResourceTable.Media_ic_avatar_woman, COLOR_PINK);

        decoView.addEvent(new DecoEvent.Builder(64)
                .setIndex(mSeries1Index)
                .setDelay(11000)
                .setDuration(5000)
                .build());

        addAnimation(decoView, mSeries3Index, 36, 19000, imageViewAvatar, ResourceTable.Media_ic_avatar_child, COLOR_YELLOW);

        decoView.addEvent(new DecoEvent.Builder(79)
                .setIndex(mSeries2Index)
                .setDelay(19000)
                .setDuration(5000)
                .build());

        decoView.addEvent(new DecoEvent.Builder(100)
                .setIndex(mSeries1Index)
                .setDelay(19000)
                .setDuration(5000)
                .build());

        decoView.addEvent(new DecoEvent.Builder(DecoEvent.EventType.EVENT_COLOR_CHANGE, COLOR_BACK)
                .setIndex(mBack1Index)
                .setDelay(27000)
                .setDuration(2000)
                .setListener(new DecoEvent.ExecuteEventListener() {
                    @Override
                    public void onEventStart(DecoEvent event) {
                        imageViewAvatar.setImageElement(Utility.getDrawable(getApplicationContext(), ResourceTable.Media_ic_avatar_group));
                        showAvatar(true, imageViewAvatar);
                    }

                    @Override
                    public void onEventEnd(DecoEvent event) {

                    }
                })
                .build());

        addFinishAnimation(decoView, mSeries3Index, 1250, 30000, imageViewAvatar);
        addFinishAnimation(decoView, mSeries2Index, 1500, 30000, null);
        addFinishAnimation(decoView, mSeries1Index, 1750, 30000, null);
    }

    private void addFinishAnimation(final DecoView arcView, final int series, final int duration, int delay, final Component view) {
        arcView.addEvent(new DecoEvent.Builder(0)
                .setIndex(series)
                .setDelay(delay)
                .setDuration(duration)
                .setListener(new DecoEvent.ExecuteEventListener() {
                    @Override
                    public void onEventStart(DecoEvent event) {
                        arcView.getChartSeries(series).getSeriesItem().setSeriesLabel(null);
                    }

                    @Override
                    public void onEventEnd(DecoEvent event) {
                        if (view != null) {
                            showAvatar(false, view);
                        }
                    }
                })
                .build());
    }

    private void addAnimation(final DecoView arcView,
                              int series, float moveTo, int delay,
                              final Image imageView, final int imageId,
                              final int color) {
        DecoEvent.ExecuteEventListener listener = new DecoEvent.ExecuteEventListener() {
            @Override
            public void onEventStart(DecoEvent event) {
                imageView.setImageElement(Utility.getDrawable(getApplicationContext(), imageId));

                showAvatar(true, imageView);

                arcView.addEvent(new DecoEvent.Builder(DecoEvent.EventType.EVENT_COLOR_CHANGE, color)
                        .setIndex(mBack1Index)
                        .setDuration(2000)
                        .build());
            }

            @Override
            public void onEventEnd(DecoEvent event) {
                showAvatar(false, imageView);

                arcView.addEvent(new DecoEvent.Builder(DecoEvent.EventType.EVENT_COLOR_CHANGE, COLOR_BACK)
                        .setIndex(mBack1Index)
                        .setDuration(2000)
                        .build());
            }

        };

        arcView.addEvent(new DecoEvent.Builder(moveTo)
                .setIndex(series)
                .setDelay(delay)
                .setDuration(5000)
                .setListener(listener)
                .build());
    }

    private void showAvatar(boolean show, Component view) {
        if(show)
            view.setVisibility(Component.VISIBLE);
        else
            view.setVisibility(Component.INVISIBLE);
    }
}